package com.ops.chirpit;

import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomBaseAdapter extends BaseAdapter {

	 private static JSONArray searchArrayList;
	 private static int mListType;
	 private InputStream mDiskCache;
	 private LayoutInflater mInflater;

	 public CustomBaseAdapter(Context context, JSONArray friendList, int listType) {
	  searchArrayList = friendList;
	  mInflater = LayoutInflater.from(context);
	  mListType = listType;
	 }

	 public int getCount() {
	  return searchArrayList.length();
	 }

	 public Object getItem(int position) {
	  try {
		return searchArrayList.get(position);
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
	 }

	 public long getItemId(int position) {
	  return position;
	 }

	 public View getView(int position, View convertView, ViewGroup parent) {
	  ViewHolder holder;
	  if (convertView == null) {
	   convertView = mInflater.inflate(R.layout.main_list_item, null);
	   holder = new ViewHolder();
       holder.icon1 = (ImageView) convertView.findViewById(R.id.icon1);
       holder.title = (TextView) convertView.findViewById(R.id.title);
       holder.line1 = (TextView) convertView.findViewById(R.id.line1);
       holder.icon2 = (ImageView) convertView.findViewById(R.id.icon2);
       holder.line2 = (TextView) convertView.findViewById(R.id.line2);
       holder.icon3 = (ImageView) convertView.findViewById(R.id.icon3);
       holder.venueCheckinCount = (TextView) convertView.findViewById(R.id.venueCheckinCount);
       holder.todoHere = (ImageView) convertView.findViewById(R.id.venueTodoCorner);
       holder.row3 = (LinearLayout) convertView.findViewById(R.id.mli_row3);

	   convertView.setTag(holder);
	  } else {
	   holder = (ViewHolder) convertView.getTag();
	  }
	  
	  try {
		  JSONObject item = (JSONObject) getItem(position);
		  if (mListType == ChirpItActivity.FRIENDS){
		      holder.icon1.setImageResource(R.drawable.ic_contact_picture);
			  holder.title.setText(item.getString("name"));
			  holder.icon2.setVisibility(View.GONE);
			  holder.icon3.setVisibility(View.GONE);
			  int drawable = 0;
			  switch(item.getInt("provider")){
			  	case ChirpItActivity.FACEBOOK:
				  drawable = R.drawable.facebook;
				  break;
			  	case ChirpItActivity.TWITTER:
			  		drawable = R.drawable.twitter;
			  		break;
			  }
			  if (drawable > 0){
				  holder.todoHere.setImageResource(drawable);
				  holder.todoHere.setVisibility(View.VISIBLE);
			  }
			  holder.row3.setVisibility(View.GONE);
		  }
		  else {
			  int drawable = AccountType.getDrawable(item.getInt("provider"));
			  holder.icon1.setImageResource(drawable);
			  /*Category category = venue.getString("category");
		        if (category != null) {
		            Uri photoUri = Uri.parse(category.getIcon());
		            try {
		                Bitmap bitmap = BitmapFactory.decodeStream(mDiskCache.getInputStream(Uri.encode(photoUri.toString())));
		                holder.icon.setImageBitmap(bitmap);
		            } catch (IOException e) {
		                setDefaultVenueCategoryIcon(holder);
		            }
		        } else {
		            // If there is no category for this venue, fall back to the original
		            // method of the
		            // blue/grey pin depending on if the user has been there or not.
		            setDefaultVenueCategoryIcon(holder);
		        }*/
			holder.title.setText(item.getString("name"));
			holder.line1.setVisibility(View.GONE);//setText(item.getString("provider"));
			// Show venue distance.
	        if (item.getString("distance") != null) {
	            holder.line2.setText(item.getString("distance") + " meters");
	        } else {
	            holder.line2.setVisibility(View.GONE);
	        }
			// If there's a special here, show the special here icon.
	        //if (VenueUtils.getSpecialHere(venue)) {
	            holder.icon2.setVisibility(View.GONE);
	        //} else {
	            holder.icon3.setVisibility(View.GONE);
	        //}
		  }
		} catch (JSONException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
		}
	  return convertView;
	 }
	 
	 private void setDefaultVenueCategoryIcon(ViewHolder holder) {
	        holder.icon1.setImageResource(R.drawable.category_none);
	    }

	 static class ViewHolder {
	        ImageView icon1;
	        TextView title;
	        TextView line1;
	        ImageView icon2;
	        TextView line2;
	        ImageView icon3;
	        TextView venueCheckinCount;
	        ImageView todoHere;
	        LinearLayout row3;
	 }

	}